package cn.hdustea.shopNavigation.ui.view.main

import android.content.Context
import android.content.Intent
import android.view.View
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import cn.hdustea.shopNavigation.R
import cn.hdustea.shopNavigation.config.ConstantUrl
import cn.hdustea.shopNavigation.entity.Education
import cn.hdustea.shopNavigation.ui.contract.EducationContract
import cn.hdustea.shopNavigation.ui.presenter.EducationPresenter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hdustea.base.base.BaseActivity
import com.hdustea.base.widget.CustomLoadMoreView
import com.hdustea.base.widget.SpacesItemDecoration
import kotlinx.android.synthetic.main.activity_education.*
import kotlinx.android.synthetic.main.item_list_education.view.*

/**
 * <pre>
 *     author : HuQiang
 *     time   : 2020/09/22
 *     desc   :
 *     version: 1.0
 * </pre>
 */
class EducationActivity : BaseActivity<EducationContract.Presenter>(), EducationContract.View {

    private var educationAdapter: EducationItemAdapter? = null

    private var mCurrentPage: Int = 0

    companion object {

        fun startActivity(context: Context) {
            context.startActivity(Intent(context, EducationActivity::class.java))
        }

    }

    override fun initView() {
        Glide.with(this)
                .load("${ConstantUrl.BASE_URL}/image/banner/special.jpg")
                .into(im_banner)
        val layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        recycler_view.layoutManager = layoutManager
        recycler_view.isNestedScrollingEnabled = true
        recycler_view.addItemDecoration(SpacesItemDecoration(16))

        showLoading()
    }

    override fun initData() {
        mPresenter?.getEducationList(0, false)
    }

    private fun initRecyclerView() {
        educationAdapter!!.animationEnable = true
        educationAdapter!!.loadMoreModule.loadMoreView = CustomLoadMoreView()
        educationAdapter!!.loadMoreModule.setOnLoadMoreListener {
            mPresenter?.getEducationList(mCurrentPage + 1, true)
        }
        educationAdapter!!.loadMoreModule.isAutoLoadMore = true
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        educationAdapter!!.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
    }

    override fun getLayoutRes(): Int = R.layout.activity_education

    override fun getPresenter(): EducationContract.Presenter = EducationPresenter(this)

    override fun setEducationList(educationList: MutableList<Education>, isRefresh: Boolean) {
        if (!isRefresh) {
            educationAdapter = EducationItemAdapter(R.layout.item_list_education)
            initRecyclerView()
            recycler_view.adapter = educationAdapter
            educationAdapter!!.setList(educationList)
        } else {
            if (educationList.isNotEmpty()) {
                mCurrentPage++
                educationAdapter!!.addData(educationList)
                educationAdapter!!.loadMoreModule.loadMoreComplete()
            } else {
                educationAdapter!!.loadMoreModule.loadMoreEnd()
            }
        }
    }

    override fun onError() {
        educationAdapter!!.loadMoreModule.isEnableLoadMore = true
        educationAdapter!!.loadMoreModule.loadMoreFail()
    }


    inner class EducationItemAdapter(layoutRes: Int) :
            BaseQuickAdapter<Education, BaseViewHolder>(layoutRes), LoadMoreModule {

        private val OPTIONS: RequestOptions = RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .dontAnimate()

        override fun convert(holder: BaseViewHolder, item: Education) {
            val rootView = holder.itemView
            Glide.with(context).load(item.picurl).apply(OPTIONS).into(rootView.im_education)
            rootView.tv_education_name.text = item.name
            rootView.tv_education_type.text = item.tag
            if (item.consume_per == 0) {
                rootView.tv_education_average_price.visibility = View.GONE
            } else {
                rootView.tv_education_average_price.text = String.format(context.resources.getString(R.string.food_price), item.consume_per)
                rootView.tv_education_average_price.visibility = View.VISIBLE
            }
            rootView.tv_education_location.text = item.region
        }
    }


}